home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / mep1 / External Functions / XCMDs⁄XFCNs / C Sources / PlaySound.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-24  |  1.9 KB  |  80 lines  |  [TEXT/KAHL]

  1. /*
  2.  *  playSound -- XFCN to play a sound
  3.  *
  4.  *  callout("XFCN", "PlaySound", "Sound")
  5.  *
  6.  *  where "Sound" is the name of a resource of type 'snd '.
  7.  *
  8.  *  The caller should make sure that the resource fork that
  9.  *  contains the desired sound is open.
  10.  */
  11.  
  12. #include <SetupA4.h>
  13. #include <SoundMgr.h>
  14. #include "IconXCmd.h"
  15.  
  16.  
  17. #define    NIL    0x00000000
  18.  
  19. pascal void main(paramPtr)
  20. XCmdBlockPtr    paramPtr;
  21.     {
  22.     SndChannelPtr    tempChan, theChan;
  23.     Handle            hSnd;
  24.     OSErr            err;
  25.     Handle            h;
  26.  
  27.     RememberA0();                /* set up for globals */
  28.     SetUpA4();
  29.  
  30.     /* check parameters */
  31.     if (paramPtr->paramCount != 1) {
  32.         paramPtr->returnValue = (Handle)NewString("\pNeed one parameter in PlaySound XFCN");
  33.         PtoCstr(*(paramPtr->returnValue));
  34.         RestoreA4();
  35.         return;
  36.         }
  37.     
  38.     /* Lock parameter, convert to a Pascal string */
  39.     h = paramPtr->params[0];
  40.     HLock(h);
  41.     CtoPstr(*h);
  42.     hSnd = GetNamedResource( (ResType)'snd ', *h );
  43.     HUnlock(h);
  44.  
  45.     if (!hSnd) {
  46.         paramPtr->returnValue = (Handle)NewString("\pNo such sound in PlaySound XFCN");
  47.         PtoCstr(*(paramPtr->returnValue));
  48.         RestoreA4();
  49.         return;
  50.         }
  51.  
  52.     tempChan = NIL;                    /* open new channel */
  53.     err = SndNewChannel(&tempChan, 0, 0L, NIL);
  54.     if (err) {
  55.         paramPtr->returnValue = (Handle)NewString("\pChannel opening error in PlaySound XFCN");
  56.         PtoCstr(*(paramPtr->returnValue));
  57.         ReleaseResource(hSnd);        /* release resource */
  58.         RestoreA4();
  59.         return;
  60.         }
  61.         
  62.     /* If a sound channel was already open, nextChan will have a pointer to it 
  63.      * in tempChan->nextChan; if not nextChan will contain NIL.
  64.      */
  65.         
  66.     theChan = tempChan->nextChan;                /* get pointer to open channel, if any */
  67.     err = SndDisposeChannel(tempChan, FALSE);    /* close ours */
  68.  
  69.     /* Play the sound. If a channel was open, it will be used. If not, theChan will
  70.      * be NIL and SndPlay will create a temporary channel.
  71.      */    
  72.     HLock(hSnd);
  73.     err = SndPlay(theChan, hSnd, FALSE);    /* play sound */
  74.     HUnlock(hSnd);
  75.     ReleaseResource(hSnd);
  76.             
  77.     RestoreA4();
  78.     }
  79.  
  80.